package com.bonana.backend.session;

import com.bonana.core.entity.BackendUser;
import com.bonana.core.entity.CoreUser;
import com.bonana.core.entity.PartnerUser;
import com.bonana.utils.ConvertUtils;

import javax.print.attribute.IntegerSyntax;
import javax.servlet.http.HttpSession;


public class BackendSession {

    public static Object getAttribute(HttpSession session, String key) {
        return session.getAttribute(key);
    }

    public static void saveSession(HttpSession session, BackendUser backendUser) {
        session.setAttribute("session.uid", backendUser.getUid());
        session.setAttribute("session.username", backendUser.getUsername());
        session.setAttribute("session.name", backendUser.getName());
        session.setAttribute("session.usertype", 1);

    }

    public static void saveSession(HttpSession session, PartnerUser partnerUser) {
        session.setAttribute("session.uid", partnerUser.getUid());
        session.setAttribute("session.username", partnerUser.getUsername());
        session.setAttribute("session.name", partnerUser.getNickname());
        session.setAttribute("session.usertype", 2);

    }

    public static void saveSession(HttpSession session, CoreUser coreUser) {
        session.setAttribute("session.uid", coreUser.getUid());
        session.setAttribute("session.username", coreUser.getUsername());
        session.setAttribute("session.usertype", 3);
        session.setMaxInactiveInterval(7*24*60*60);
    }

    public static void removeSession(HttpSession session) {
        session.removeAttribute("session.uid");
        session.removeAttribute("session.name");
        session.removeAttribute("session.username");
        session.removeAttribute("session.usertype");
    }

    public static String getUsername(HttpSession session) {
        return ConvertUtils.toString(getAttribute(session, "session.username"));
    }

    public static int getUid(HttpSession session) {
        return ConvertUtils.toInteger(getAttribute(session, "session.uid"));
    }

    /**
     * 管理员姓名
     *
     * @param session
     * @return
     */
    public static String getName(HttpSession session) {
        return ConvertUtils.toString(getAttribute(session, "session.name"));
    }

    public static Integer getType(HttpSession session) {
        return ConvertUtils.toInteger(getAttribute(session, "session.usertype"));
    }

}
